/*
 * HttpPost.cpp
 *
 */

#include "HttpPost.h"
#include "HttpHeader.h"
#include "../../common/StrUtils.h"
#include <sstream>

HttpPost::HttpPost(const std::string& uri, const std::string& protocVer):
HttpRequest(uri,protocVer), body(NULL) {}

HttpPost::~HttpPost() {
	if (body != NULL)
		delete body;
}

HttpBody& HttpPost::getBody() const {
	if (!body)
		throw MessageException("No body in POST message");
	return *body;
}

bool HttpPost::hasBody() const {
	bool result = false;
	size_t size;
	try {
		size = StrUtils::fromString<int>(getHeader(HttpHeader::CONTENT_LENGTH));
		result = (size > 0);
	}
	catch(const MessageException& e) {
		// No existe el header indicador de longitud de cuerpo
	}
	return result;
}

void HttpPost::setBody(HttpBody* body) {
	if (this->body != NULL)
		delete this->body;
	this->body = body;
}

const std::string& HttpPost::getMethod() const {
	return POST;
}
